//引入依赖的配置文件模块
require(["config"],function(){
	//引入依赖文件
	require(["jquery","header"],function($){
		function Confirm(){
			this.verify();
			this.loadAddress();
		}
		$.extend(Confirm.prototype, {
			//判断是否登陆 cookie中查看数据是够存在 不存在 则跳转到登录页面
			verify: function(){
				$.cookie.json = true;
				var user= $.cookie("login-user");
				if(!user){
					location = "/html/login.html";
				}
			},
			//加载地址数据
			loadAddress:function(){
				// 首先加载省份数据
				this.loadProvince();
				// 省份选择改变时，加载城市 区县初始化
				$(".province").on("change",this.loadCity);
				// 城市选择改变时，加载区县
				$(".city").on("change", this.loadDistrict);
			},
			loadProvince: function(){
				var url1 = "http://route.showapi.com/1149-1?showapi_appid=75543&showapi_sign=0d0306f3b2c241eeb43bd3db00a6c70c&level=1&page=1&maxSize=20",
					url2 = "http://route.showapi.com/1149-1?showapi_appid=75543&showapi_sign=0d0306f3b2c241eeb43bd3db00a6c70c&level=1&page=2&maxSize=20";
				$.when($.ajax(url1),$.ajax(url2)).done(function(data1,data2){
				  	var html = '<option value="-1">请选择省份</option>';
				  	//拼接data1与data2
				  	data1[0].showapi_res_body.data.concat(data2[0].showapi_res_body.data)
				  	//渲染数据
				  	.forEach(function(curr) {
				  		html += `<option value="${curr.id}">${curr.areaName}</option>`;
				  	});
				  	$(".province").html(html);
				});
			
			},
			loadCity: function(){
				// 获取选择的省份 id
				var id = $(".province").val();
				// 根据 id 查询城市
				//区县改变
				var html2 = '<option value="-1">请选择区县</option>';
				var url = `http://route.showapi.com/1149-2?showapi_appid=75543&showapi_sign=0d0306f3b2c241eeb43bd3db00a6c70c&parentId=${id}`;
				$.ajax(url).done(function(data) {
					var html = '<option value="-1">请选择城市</option>';
					data.showapi_res_body.data.forEach(function(curr) {
						html += `<option value="${curr.id}">${curr.areaName}</option>`;
					});
					$(".city").html(html);
				});
				$(".district").html(html2);
			},
			loadDistrict: function(){
				// 获取选择的城市 id
				var id = $(".city").val();
				// 根据 id 查询区县
				var url = `http://route.showapi.com/1149-2?showapi_appid=75543&showapi_sign=0d0306f3b2c241eeb43bd3db00a6c70c&parentId=${id}`;
				$.ajax(url).done(function(data) {
					var html = '<option value="-1">请选择区县</option>';
					data.showapi_res_body.data.forEach(function(curr) {
						html += `<option value="${curr.id}">${curr.areaName}</option>`;
					});
					$(".district").html(html);
				});
			}
		});
		
		new Confirm();
	});
});